package com.kinghood.productcenter.controller.product;

import com.kinghood.infrastructure.api.authority.SysLogModuleEnum;
import com.kinghood.infrastructure.aspect.OperatorLog;
import com.kinghood.infrastructure.model.KingHoodResponseVO;
import com.kinghood.productcenter.common.model.dto.base.ListDownloadQueryDTO;
import com.kinghood.productcenter.service.product.IDownloadCenterService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 * 下载中心 前端控制器
 * </p>
 *
 * @author KingHood
 * @since 2023-12-20
 */
@RestController
@RequestMapping("/downloadCenter")
public class DownloadCenterController {
    @Resource
    private IDownloadCenterService downloadService;

    /**
     * 列表
     *
     * @author jp
     * @date 2023/12/20
     */
    @PostMapping("/listFile")
    public KingHoodResponseVO listFile(@RequestBody ListDownloadQueryDTO param) {
        return KingHoodResponseVO.success(downloadService.listFile(param));
    }

    /**
     * 下载
     *
     * @author jp
     * @date 2023/12/20
     */
    @RequestMapping("/downloadFile/{id}")
    public void downloadFile(@PathVariable("id") Long id, HttpServletResponse response, HttpServletRequest request) {
        downloadService.downloadFile(id, response, request);
    }

    /**
     * 增加下载次数
     *
     * @author jp
     * @date 2023/12/20
     */
    @RequestMapping("/addFrequency/{id}")
    public KingHoodResponseVO addFrequency(@PathVariable("id") Long id) {
        downloadService.addFrequency(id);
        return KingHoodResponseVO.success();
    }

    /**
     * 获取模块数据
     *
     * @return {@link KingHoodResponseVO}
     */
    @RequestMapping("/listModule")
    public KingHoodResponseVO listModule() {
        return KingHoodResponseVO.success(downloadService.listModule());
    }

    /**
     * 根据id删除
     *
     * @param id id
     * @author jp
     * @date 2023/12/21
     */
    @OperatorLog(module = SysLogModuleEnum.Type.PRODUCT_DOWNLOAD,content = "下载中心管理-删除文件")
    @RequestMapping("/delFile/{id}")
    public KingHoodResponseVO delFile(@PathVariable("id") Long id) {
        downloadService.delFile(id);
        return KingHoodResponseVO.success();
    }

    /**
     * 上传文件
     *
     * @param file 文件
     * @return @return {@link KingHoodResponseVO }
     * @author jp
     * @date 2024/01/10 10:15
     */
    @RequestMapping("/uploadFile")
    public KingHoodResponseVO uploadFile(MultipartFile file) {
        downloadService.uploadFile(file);
        return KingHoodResponseVO.success();
    }
}
